package com.aloda.controller;

import com.aloda.anno.OperationLog;
import com.aloda.constant.UserConstant;
import com.aloda.pojo.dto.BookAddDTO;
import com.aloda.pojo.dto.BookBorrowDTO;
import com.aloda.pojo.dto.BookEditDTO;
import com.aloda.pojo.dto.BookPageDTO;
import com.aloda.pojo.entity.Book;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.BookService;
import com.aloda.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * 图书controller
 *
 * @author aloda
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 获取推荐图书（最新上架10本）
     *
     * @return
     */
    @GetMapping("/recommend")
    public Result<List<Book>> getRecommendBook() {
        List<Book> books = bookService.getRecommendBook();
        return Result.success(books);
    }

    /**
     * 分页查询图书
     *
     * @param bookPageDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult<Book>> getBookPage(@RequestBody BookPageDTO bookPageDTO) {
        PageResult<Book> result = bookService.getBookPage(bookPageDTO);
        return Result.success(result);
    }

    /**
     * 新增图书
     *
     * @param bookAddDTO
     * @return
     */
    @OperationLog
    @PostMapping
    public Result addBook(@RequestBody BookAddDTO bookAddDTO) {
        bookService.addBook(bookAddDTO);
        return Result.success();
    }

    /**
     * 根据id查询图书
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Book> getBook(@PathVariable Integer id) {
        Book book = bookService.getBookById(id);
        return Result.success(book);
    }

    /**
     * 编辑图书
     *
     * @param bookEditDTO
     * @return
     */
    @OperationLog
    @PutMapping
    public Result editBook(@RequestBody BookEditDTO bookEditDTO) {
        bookService.editBook(bookEditDTO);
        return Result.success();
    }

    /**
     * 借阅图书
     * @param
     * @return
     */
    @PostMapping("/borrow")
    public Result borrowBook(@RequestBody BookBorrowDTO bookBorrowDTO) {
        bookService.borrowBook(bookBorrowDTO);
        return Result.success();
    }

    /**
     * 获取当前借阅图书分页
     * @param bookPageDTO
     * @return
     */
    @PostMapping("/current")
    public Result<PageResult<Book>> getCurrentBorrowBookPage (@RequestBody BookPageDTO bookPageDTO) {
        PageResult<Book> result = bookService.getCurrentBorrowBookPage(bookPageDTO);
        return Result.success(result);
    }

    /**
     * 归还图书
     * @param id
     * @return
     */
    @PostMapping("/return/{id}")
    public Result returnBook(@PathVariable Integer id) {
        bookService.returnBook(id);
        return Result.success();
    }


    /**
     * 归还确认
     * @param id
     * @return
     */
    @PostMapping("/confirm/{id}")
    public Result returnConfirm(@PathVariable Integer id) {
        bookService.returnConfirm(id);
        return Result.success();
    }
}
